<?php

/** @var yii\web\View $this */

use yii\bootstrap5\Html;
use yii\helpers\Url;

$url_login = Url::to(['login/login']);
$url_home = Url::to(['home/index']);
$this->title = '登录';
?>
<div class="site-login">
    <div class="container vh-50">
        <div class="row vh-50">

            <div class="col-4 m-auto p-5 justify-content-center bg-white rounded">
                <h1><?= Html::encode($this->title) ?></h1>
                <form class="" role="form">
                    <div class="mb-3">
                        <label class="form-label mb-1 text-red-50">用户名：</label>
                        <input type="text" class="form-control" id="txt_user_account">
                        <label id="lb_user" class="form-label mb-1 text-danger"></label>
                    </div>
                    <div class="mb-4">
                        <label class="form-label mb-1 text-black-50">密码：</label>
                        <input type="password" class="form-control" id="txt_user_passwd">

                    </div>
                    <div class="mb-1">
                        <button type="button" class="form-control btn-primary" id="btn_login">登录</button>
                    </div>
                    <label id="lb_login" class="form-label mb-1 text-primary"></label>
                </form>
            </div>
        </div>
    </div>

    <div class="offset-lg-1" style="color:#999;">
        You may login with <strong>admin/123456</strong> or <strong>user/123456</strong>.
    </div>

</div>

<script>

    $('#btn_login').click(function () {
        var user_account = $("#txt_user_account").val();
        var user_passwd = $("#txt_user_passwd").val();

        if (user_account == "") {
            $("#lb_user").text("*用户名不能为空");
        } else {
            $("#lb_user").text("");
        }

        if (user_passwd == "") {
            $("#lb_passwd").text("*密码不能为空");
        } else {
            $("#lb_passwd").text("");
        }
        var url ="<?= $url_login ?>";
        var params = {
            "user_account":user_account,
            "user_passwd":user_passwd
        };
        $.post(url,params,function (result){
            var data = $.parseJSON(result)
            if(0==data.errCode){
                $("#lb_login").text("登录成功");
                setTimeout(function(){
                    $(location).attr('href', '<?= $url_home ?>');
                },1000);
            }else{
                $("#lb_login").text(data.msg);
            }

        });

    });

</script>
